package com.go.where.manager.service.system.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.go.where.manager.dao.system.MenuDao;
import com.go.where.manager.dao.system.PermissionDao;
import com.go.where.manager.dao.system.RoleDao;
import com.go.where.manager.exception.ManagerException;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.pojo.user.Permission;
import com.go.where.manager.pojo.user.Role;
import com.go.where.manager.service.system.RoleService;
import com.go.where.manager.util.CommonUtil;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

	private static Logger log = Logger.getLogger(RoleServiceImpl.class);
	@Resource(name = "roleDao")
	private RoleDao roleDao;
	@Resource(name = "permissionDao")
	private PermissionDao permissionDao;
	@Resource(name = "menuDao")
	private MenuDao menuDao;

	@Override
	public int delete(String roleId) {
		permissionDao.deleteByRoleId(roleId);
		return roleDao.deleteByKey(roleId);
	}

	@Override
	public Role queryById(String roleId) {
		return roleDao.getByKey(roleId);
	}

	@Override
	public Pagination<Role> queryPage(Role role) {
		Integer rowCount = roleDao.totalCount(role);
		role.calculate();
		List<Role> list = roleDao.queryList(role);
		if (list != null && !list.isEmpty()) {
			for (Role rol : list) {
				String roleId = rol.getRoleId();
				List<String> menuIdList = permissionDao.selectByRoleId(roleId);
				List<Menu> menuList = menuDao.queryMenuList(menuIdList);
				rol.setMenuList(menuList);
			}
		}
		Pagination<Role> page = roleDao.queryPagination(rowCount, list, role);
		return page;
	}

	@Override
	public int save(Role role) {
		role.setRoleId(CommonUtil.createRandomNumber());
		for (int i = 0; i < 20; i++) {
			Role rDB = roleDao.getByKey(role.getRoleId());
			if (rDB != null) {
				role.setRoleId(CommonUtil.createRandomNumber());
			} else {
				break;
			}
		}
		role.setInsertTime(new Date());
		int roleR = roleDao.insert(role);
		int result = 0;
		if (roleR > 0) {
			if (role.getMenuIdList() != null && !role.getMenuIdList().isEmpty()) {
				for (String menuId : role.getMenuIdList()) {
					Permission per = new Permission();
					per.setId(UUID.randomUUID().toString());
					per.setRoleId(role.getRoleId());
					per.setMenuId(menuId);
					per.setInsertTime(new Date());
					result = permissionDao.insert(per);
					if (result < 0) {
						log.debug("角色权限信息保存异常");
						throw new ManagerException("角色权限信息保存异常");
					}
				}
			} else {
				log.debug("该角色没有权限,角色Id：" + role.getRoleId() + "角色名称："
						+ role.getRoleName());
				result = 1;
			}
		} else {
			log.debug("角色信息保存异常");
			return -1;
		}
		return result;
	}

	@Override
	public int update(Role role) {
		role.setUpdateTime(new Date());
		int roleR = roleDao.update(role);
		int result = 0;
		if (roleR > 0) {
			int dr = permissionDao.deleteByRoleId(role.getRoleId());
			if (dr > 0) {
				if (role.getMenuIdList() != null
						&& !role.getMenuIdList().isEmpty()) {
					for (String menuId : role.getMenuIdList()) {
						Permission per = new Permission();
						per.setId(UUID.randomUUID().toString());
						per.setRoleId(role.getRoleId());
						per.setMenuId(menuId);
						per.setInsertTime(new Date());
						result = permissionDao.insert(per);
						if (result < 0) {
							log.debug("角色权限信息更新异常");
							throw new ManagerException("角色权限信息更新异常");
						}
					}
				} else {
					log.debug("角色权限不能为空，角色Id：" + role.getRoleId());
					throw new ManagerException("角色权限不能为空");
				}
			}
		} else {
			log.debug("角色信息更新异常");
			return -1;
		}
		return result;
	}

	@Override
	public List<Role> queryAll() {
		return roleDao.queryAll();
	}

}
